package com.knowledgegraph.controller;

import com.knowledgegraph.common.Result;
import com.knowledgegraph.common.PageResult;
import com.knowledgegraph.entity.RelationType;
import com.knowledgegraph.service.RelationTypeService;
import com.knowledgegraph.vo.RelationTypeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 关系类型控制器
 */
@RestController
@RequestMapping("/relation-types")
@CrossOrigin
public class RelationTypeController {

    @Autowired
    private RelationTypeService relationTypeService;

    /**
     * 分页查询关系类型
     */
    @PostMapping("/list")
    public Result<PageResult<RelationTypeVO>> getRelationTypes(@RequestBody Map<String, Object> params) {
        Integer page = params.get("page") != null ? Integer.parseInt(params.get("page").toString()) : 1;
        Integer pageSize = params.get("pageSize") != null ? Integer.parseInt(params.get("pageSize").toString()) : 10;
        String searchText = params.get("searchText") != null ? params.get("searchText").toString() : null;
        Boolean onlyCurrentUser = params.get("onlyCurrentUser") != null ? Boolean.parseBoolean(params.get("onlyCurrentUser").toString()) : false;
        
        PageResult<RelationTypeVO> result = relationTypeService.getRelationTypes(page, pageSize, searchText, onlyCurrentUser);
        return Result.success(result);
    }

    /**
     * 获取关系类型详情
     */
    @GetMapping("/{id}")
    public Result<RelationTypeVO> getRelationTypeById(@PathVariable Long id) {
        RelationTypeVO relationType = relationTypeService.getRelationTypeById(id);
        if (relationType == null) {
            return Result.fail("关系类型不存在");
        }
        return Result.success(relationType);
    }

    /**
     * 创建关系类型
     */
    @PostMapping
    public Result<Long> createRelationType(@RequestBody RelationType relationType) {
        try {
            Long id = relationTypeService.createRelationType(relationType);
            return Result.success("创建成功", id);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 更新关系类型
     */
    @PutMapping
    public Result<Boolean> updateRelationType(@RequestBody RelationType relationType) {
        try {
            Boolean success = relationTypeService.updateRelationType(relationType);
            return success ? Result.success("更新成功", true) : Result.fail("更新失败");
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 删除关系类型
     */
    @PostMapping("/delete/{id}")
    public Result<Boolean> deleteRelationType(@PathVariable Long id) {
        try {
            Boolean success = relationTypeService.deleteRelationType(id);
            return success ? Result.success("删除成功", true) : Result.fail("删除失败");
        } catch (Exception e) {
            return Result.fail("删除失败：" + e.getMessage());
        }
    }

    /**
     * 批量删除关系类型
     */
    @PostMapping("/batch-delete")
    public Result<Integer> batchDeleteRelationTypes(@RequestBody List<Long> ids) {
        try {
            Integer count = relationTypeService.batchDeleteRelationTypes(ids);
            return Result.success("成功删除" + count + "条记录", count);
        } catch (Exception e) {
            return Result.fail("批量删除失败：" + e.getMessage());
        }
    }

    /**
     * 更新关系类型排序
     */
    @PutMapping("/{id}/sort-order")
    public Result<Boolean> updateSortOrder(@PathVariable Long id, @RequestParam Integer sortOrder) {
        Boolean success = relationTypeService.updateSortOrder(id, sortOrder);
        return success ? Result.success("更新成功", true) : Result.fail("更新失败");
    }

    /**
     * 更新关系类型启用状态
     */
    @PutMapping("/{id}/enabled")
    public Result<Boolean> updateEnabled(@PathVariable Long id, @RequestParam Boolean enabled) {
        Boolean success = relationTypeService.updateEnabled(id, enabled);
        return success ? Result.success("更新成功",true) : Result.fail("更新失败");
    }

    /**
     * 获取所有启用的关系类型
     */
    @GetMapping("/enabled")
    public Result<List<RelationTypeVO>> getAllEnabledTypes() {
        List<RelationTypeVO> types = relationTypeService.getAllEnabledTypes();
        return Result.success(types);
    }

    /**
     * 获取当前用户创建的关系类型
     */
    @GetMapping("/user")
    public Result<List<RelationTypeVO>> getCurrentUserTypes() {
        List<RelationTypeVO> types = relationTypeService.getCurrentUserTypes();
        return Result.success(types);
    }
}